package com.hns.can.mainte.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hns.tool.pub.SearchConditionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.domain.ReturnResult;
import com.hns.can.common.web.BaseController;
import com.hns.can.mainte.entity.UpgEqmtVrsnManageInfo;
import com.hns.can.mainte.service.AirConditionerService;
import com.hns.can.mainte.service.CommonUpgradeService;
import com.hns.can.mainte.vo.AppUpgradeSearchVo;
import com.hns.can.mainte.vo.UpgradeVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.pub.PubUtil;

/**
 * 空调Controller
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/airConditioner")
public class AirConditionerController extends BaseController{
	
    @Autowired
    private CommonUpgradeService commonUpgradeService; // 注入commonUpgradeService
	
	@Autowired
    private AirConditionerService airConditionerService; 
	
	/**
	 * 新增空调模版
	 * @param entity 空调模版实体
	 * @return
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
    @RequestMapping("/addAirTemplate")
    @ResponseBody
    public Object addAirTemplate(UpgEqmtVrsnManageInfo entity) throws Exception {
    	airConditionerService.addAirTemplate(entity, this.getCurrentUser().getUser());
    	return getReturnMsg("新增空调模版成功");
    }
    
    /**
     * 删除空调模版
     * @param vrsnId 空调模版Id
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/delAirTemplate")
    @ResponseBody
    public Object delAirTemplate(String vrsnId) throws Exception {
        airConditionerService.delAirTemplate(vrsnId);
    	return getReturnMsg("删除空调模版成功");
    }
    
    /**
     * 更新空调模版
     * @param entity 空调模版实体
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/updateAirTemplate")
    @ResponseBody
    public Object updateAirTemplate(UpgEqmtVrsnManageInfo entity) throws Exception {
    	airConditionerService.updateAirTemplate(entity,this.getCurrentUser().getUser());
    	return getReturnMsg("更新空调模版成功");
    }
    
    /**
     * 获取空调模版
     * @param vrsnId 空调模版Id
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/getAirTemplateById")
    @ResponseBody
    public Object getAirTemplateById(String vrsnId) throws Exception {
    	UpgEqmtVrsnManageInfo entity=airConditionerService.getAirTemplateById(vrsnId);
    	return this.getReturnResult(entity);
    }
    
    /**
     * 
     * @param airTemplateName 空调模版名称
     * @param appUpgrade 版本类型
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchAirTemplatePage")
    @ResponseBody
    public Object searchAirTemplatePage(String airTemplateName, String appUpgrade) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("airTemplateName", airTemplateName);
        paramMap.put("appUpgrade", appUpgrade);
        Pagination page = airConditionerService.searchAirSetPage(paramMap,this.getCurrentUser().getUser());
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchAirTemplateMonitor")
    @ResponseBody
    public Object searchAirTemplateMonitor(UpgradeVo vo)throws Exception{
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        Pagination page =  airConditionerService.searchAirTemplateMonitor(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/getVersionName")
    @ResponseBody
    public Object getVersionName( )throws Exception {
        UpgEqmtVrsnManageInfo entity = new UpgEqmtVrsnManageInfo();
        SysUserEntity user = this.getCurrentUser().getUser();
        entity.setCorpId(user.getCorpId());
        List<UpgEqmtVrsnManageInfo> list = airConditionerService.getVersionName(entity);
        return this.getReturnResult(list);
    }

    /**
     *
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/setAirTemplateForCar")
    @ResponseBody
    public Object setAirTemplateForCar(UpgradeVo vo)throws Exception{
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        commonUpgradeService.setAirTemplateForCar(vo, user);
        return this.getReturnResult();
    }
    
    /**
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/airTemplateMonitorUpgrade")
    @ResponseBody
    public Object airTemplateMonitorUpgrade(UpgradeVo vo)throws Exception{
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        commonUpgradeService.saveAirpgLog(vo, user);
        return this.getReturnResult();
    }
    
    /**
     * 分页查询空调参数下发日志
     * @param conditionVo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchAirMonitorLogPage")
    @ResponseBody
    public Object searchAirMonitorLogPage(AppUpgradeSearchVo conditionVo) throws Exception {
        if(PubUtil.isEmpty(conditionVo.getOrgIds()) || PubUtil.isEmpty(conditionVo.getBeginTime()) || 
                    PubUtil.isEmpty(conditionVo.getEndTime())){
            return null;
        }
        SysUserEntity user = this.getCurrentUser().getUser();
        conditionVo.setCorpId(user.getCorpId());
        Pagination page = airConditionerService.searchAppUpgLogPage(conditionVo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    @RequestMapping("/getTCommMftCopaAir")
    @ResponseBody
    public Object getTCommMftCopaAir() throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        Object value = airConditionerService.getTCommMftCopaAir();
        result.putAll((Map<? extends String, ? extends Object>) value);
        return this.getReturnResult(result);
    }

    @RequestMapping("/templateAirConditioIssued")
    @ResponseBody
    public Object templateAirConditioIssued(UpgEqmtVrsnManageInfo entity,SearchConditionVo vo)throws Exception {
        airConditionerService.templateAirConditioIssued(entity,vo);
        return this.getReturnResult();
    }
    
}
